package com.tanhua.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.model.db.Analysis;
import com.tanhua.model.db.Dashboard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DashboardService {

    @Autowired
    private AnalysisMapper analysisMapper;

    //新增、活跃用户、次日留存率
    @GetMapping("/summary")
    public Map summary() {
        Map map = new LinkedHashMap();
        //1累计用户 求所有注册的和
        Integer countNumRegistered = analysisMapper.getCountNumRegistered();
        map.put("cumulativeUsers",countNumRegistered);
        //2过去30天活跃用户
        String lastMonth = getTime(new Date(),"month",-1);
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        Integer countNumMonthActice = analysisMapper.getCountNumActice(lastMonth, today);
        map.put("activePassMonth",countNumMonthActice);
        //3过去7天活跃用户
        String lastWeek = getTime(new Date(),"day",-6);
        Integer countNumWeekActice = analysisMapper.getCountNumActice(lastWeek,today);
        map.put("activePassWeek",countNumWeekActice);
        //4今日新增用户
        Integer newUsersToday = analysisMapper.getNewUsers(today);
        map.put("newUsersToday",newUsersToday);
        //5今日新增用户涨跌率，单位百分数，正数为涨，负数为跌
        //5.1先获取昨天的新增用户
        String yesterday = getTime(new Date(),"day",-1);
        Integer newUsersYesterday = analysisMapper.getNewUsers(yesterday);
        //5.2求涨跌率
        Integer newUsersTodayRate;
        if (newUsersYesterday == null || newUsersYesterday == 0){
            newUsersTodayRate = 0 ;
        }else {
            newUsersTodayRate = (newUsersToday - newUsersYesterday) * 100 / newUsersYesterday ;
        }
        map.put("newUsersTodayRate",newUsersTodayRate);
        //6今日登录次数
        Integer loginTimesToday = analysisMapper.getLoginTimes(today);
        map.put("loginTimesToday",loginTimesToday);
        //7今日登录次数涨跌率，单位百分数，正数为涨,负数为跌
        Integer loginTimesYesterday = analysisMapper.getLoginTimes(yesterday);
        Integer loginTimesTodayRate;
        if (newUsersYesterday == null || newUsersYesterday == 0){
            loginTimesTodayRate = 0 ;
        }else {
            loginTimesTodayRate = (loginTimesToday - loginTimesYesterday) * 100 / loginTimesYesterday;
        }
        map.put("loginTimesTodayRate",loginTimesTodayRate);
        //8今日活跃用户
        Integer activeUsersToday = analysisMapper.getActiveUsers(today);
        map.put("activeUsersToday",activeUsersToday);
        //今日活跃用户涨跌率，单位百分数，正数为涨，负数为跌
        Integer activeUsersYesterday = analysisMapper.getActiveUsers(yesterday);
        Integer activeUsersTodayRate;
        if (activeUsersYesterday == null || activeUsersYesterday == 0){
            activeUsersTodayRate = 0 ;
        }else {
            activeUsersTodayRate = (activeUsersToday - activeUsersYesterday) * 100 / activeUsersYesterday;
        }
        map.put("activeUsersTodayRate" , activeUsersTodayRate);
        return map;
    }

    //新增、活跃用户、次日留存率
    public Map users(Long sd, Long ed, String type) {
        //转换参数格式
        String startDateThisYear = new SimpleDateFormat("yyyy-MM-dd").format(new Date(sd));
        String endDateThisYear = new SimpleDateFormat("yyyy-MM-dd").format(new Date(ed));
        //去年的开始时间
        String startDateLastYear = getTime(new Date(sd),"year",-1);
        //去年的结束
        String endDateLastYear = getTime(new Date(ed),"year" , -1);
        List<Analysis> thisYearList = analysisMapper.getBetweenTime(startDateThisYear,endDateThisYear);
        List<Analysis> listYearList = analysisMapper.getBetweenTime(startDateLastYear,endDateLastYear);
        //查询完成后需要组装相应的集合
        List thisYear = inint(thisYearList, type);
        List lastYear = inint(listYearList, type);
        Map map = new HashMap();
        map.put("thisYear",thisYear);
        map.put("lastYear",lastYear);
        //判断type type	101 新增 num_registered 102 活跃用户 num_active 103 次日留存率 num_retention1d
        return map;
    }
    //抽取提取数据的方法
    public List inint(List<Analysis> list , String type){
        List result = new ArrayList();
        int index = 1;
        if (type.equals("101")){
            for (Analysis analysis : list) {
                result.add( new Dashboard(index + "",analysis.getNumRegistered()));
                index++;
            }
        }else if (type.equals("102")){
            for (Analysis analysis : list) {
                result.add( new Dashboard(index + "",analysis.getNumActive()));
                index++;
            }
        } else {
            for (Analysis analysis : list) {
                result.add( new Dashboard(index + "",analysis.getNumRetention1d()));
                index++;
            }
        }
        return result;
    }
    //抽取获取时间的方法
    public String getTime(Date date,String type , int num){
        String result = null;
        Calendar calendarSd = Calendar.getInstance();
        calendarSd.setTime(date);
        if (type.equals("year")){
            calendarSd.add(Calendar.YEAR,num);
            result = new SimpleDateFormat("yyyy-MM-dd").format(calendarSd.getTime());
        }else if (type.equals("month")){
            calendarSd.add(Calendar.MONTH,num);
            result = new SimpleDateFormat("yyyy-MM-dd").format(calendarSd.getTime());
        }else if (type.equals("day")){
            calendarSd.add(Calendar.DAY_OF_YEAR,num);
            result = new SimpleDateFormat("yyyy-MM-dd").format(calendarSd.getTime());
        }
        return result;
    }
}
